package com.example.controller;

import com.example.service.UserService;
import com.example.domain.Result;
import com.example.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/user")
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    /**
     * 登出
     */
    @PostMapping("/logout")
    public void logout(HttpSession session) {
        session.invalidate();
    }

    /**
     * 修改个人信息
     */
    @PostMapping("/owner")
    public Result owner(User user, HttpSession session) {
        User loginUser = (User) session.getAttribute("login");
        user.setUserId(loginUser.getUserId());
        userService.updateUserById(user);
        return success();
    }

    /**
     * 查询个人信息
     */
    @GetMapping("/info")
    public Result info(HttpSession session) {
        User loginUser = (User) session.getAttribute("login");
        User user = userService.selectUserById(loginUser.getUserId());
        return success(user);
    }
}
